﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 账单记录数据访问层
    /// </summary>
    public class Account
    {
        /// <summary>
        /// 增加一条账单记录
        /// </summary>
        /// <param name="accountInfo">账单实体类</param>
        /// <returns>返回账单记录ID</returns>
        public int Add(AccountInfo accountInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO ac_Account(");
            strSql.Append("UId,Mark,Tag,Describe,RecordTime,Moneys,Direction,CreateTime)");
            strSql.Append(" VALUES (");
            strSql.Append("@UId,@Mark,@Tag,@Describe,@RecordTime,@Moneys,@Direction,@CreateTime)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UId", SqlDbType.Int,4),
					new SqlParameter("@Mark", SqlDbType.Bit,1),
					new SqlParameter("@Tag", SqlDbType.VarChar,32),
					new SqlParameter("@Describe", SqlDbType.VarChar,512),
					new SqlParameter("@RecordTime", SqlDbType.DateTime),
					new SqlParameter("@Moneys", SqlDbType.Money,8),
					new SqlParameter("@Direction", SqlDbType.Int,4),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = accountInfo.UId;
            parameters[1].Value = accountInfo.Mark;
            parameters[2].Value = accountInfo.Tag;
            parameters[3].Value = accountInfo.Describe;
            parameters[4].Value = accountInfo.RecordTime;
            parameters[5].Value = accountInfo.Moneys;
            parameters[6].Value = accountInfo.Direction;
            parameters[7].Value = accountInfo.CreateTime;
            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 放入回收站/恢复账单通过账单记录ID
        /// </summary>
        /// <param name="aId">账单记录ID</param>
        /// <param name="state">状态修改 0恢复 1删除</param>
        /// <returns>返回是否成功</returns>
        public bool DelOrReAccountByID(int aId, int state)
        {
            string strSql = "UPDATE ac_Account SET  IsDelete = @State WHERE AId = @AId";
            SqlParameter[] parameters = {
                    new SqlParameter("@State", SqlDbType.Bit,4),
					new SqlParameter("@AId", SqlDbType.Int,4)
			};
            parameters[0].Value = state;
            parameters[1].Value = aId;
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取一个账单记录通过ID
        /// </summary>
        /// <param name="aId"></param>
        /// <returns></returns>
        public AccountInfo GetAccountInfoByID(int aId)
        {
            string strSql = "select top 1 AId,UId,Mark,Tag,Describe,RecordTime,Moneys,Direction,CreateTime,CheckState,CheckTime,MId from ac_Account  where AId=@AId";
            SqlParameter[] parameters = {
					new SqlParameter("@AId", SqlDbType.Int,4)
			};
            parameters[0].Value = aId;
            CaiBaby.Entity.AccountInfo model = new CaiBaby.Entity.AccountInfo();
            DataSet ds = SQLHelper.ExecuteDataSet(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql, parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.AId = int.Parse(ds.Tables[0].Rows[0]["AId"].ToString());
                model.UId = int.Parse(ds.Tables[0].Rows[0]["UId"].ToString());
                if ((ds.Tables[0].Rows[0]["Mark"].ToString() == "1") || (ds.Tables[0].Rows[0]["Mark"].ToString().ToLower() == "true"))
                {
                    model.Mark = true;
                }
                else
                {
                    model.Mark = false;
                }
                model.Tag = ds.Tables[0].Rows[0]["Tag"].ToString();
                model.Describe = ds.Tables[0].Rows[0]["Describe"].ToString();
                model.RecordTime = DateTime.Parse(ds.Tables[0].Rows[0]["RecordTime"].ToString());
                model.Moneys = decimal.Parse(ds.Tables[0].Rows[0]["Moneys"].ToString());
                model.Direction = int.Parse(ds.Tables[0].Rows[0]["Direction"].ToString());
                model.CreateTime = DateTime.Parse(ds.Tables[0].Rows[0]["CreateTime"].ToString());
                model.CheckState = int.Parse(ds.Tables[0].Rows[0]["CheckState"].ToString());
                model.CheckTime = DateTime.Parse(ds.Tables[0].Rows[0]["CheckTime"].ToString());
                model.MId = int.Parse(ds.Tables[0].Rows[0]["MId"].ToString());
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 账单记录审核不通过
        /// </summary>
        /// <param name="aIdlist">ID集合 例：1,2,3,4</param>
        /// <returns>返回是否成功</returns>
        public bool FailAccountByIDList(string aIdlist)
        {
            string strSql = "UPDATE ac_Account SET  CheckState = 2 WHERE AId = in (" + aIdlist + ")";
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 账单记录通过审核
        /// </summary>
        /// <param name="aIdlist">ID集合 例：1,2,3,4</param>
        /// <returns>返回是否成功</returns>
        public bool PassAccountByIDList(string aIdlist)
        {
            string strSql = "UPDATE ac_Account SET  CheckState = 1 WHERE AId = in (" + aIdlist + ")";
            int rows = SQLHelper.ExecuteSql(ConfigHelper.CaiBabyDBConnStr, CommandType.Text, strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
